﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class StateMachineRuleFieldELBDao : DaoService<TRuleField>
    {
        public override string TableName => "FAS_StateMachineRuleField";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "RuleID";
        public string MetaDataForeignColumn => "StateMachineID";
        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<TRuleField>();

        public StateMachineRuleFieldELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<TRuleField> GetTRuleFields(string ruleID)
        {
            try
            {
                var result = new List<TRuleField>();
                if (string.IsNullOrEmpty(ruleID))
                {
                    return result;
                }
                var dt = base.GetByFilter($"{ForeignColumn}='{ruleID}'");
                result = DaoUtil.ConvertToEntity<TRuleField>(dt, this.ExceptColumnName) ?? result;
                return result.OrderBy(o => o.Sort).ToList();
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.RetrieveRuleError, e.Message, e);
            }
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<TRuleField> GetByMetaDataForeignKey(string smID)
        {
            try
            {
                var result = new List<TRuleField>();
                if (string.IsNullOrEmpty(smID))
                {
                    return result;
                }
                var dt = base.GetByFilter($"{MetaDataForeignColumn}='{smID}'");
                result = DaoUtil.ConvertToEntity<TRuleField>(dt, this.ExceptColumnName) ?? result;
                return result.OrderBy(o => o.Sort).ToList();
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.RetrieveRuleError, e.Message, e);
            }
        }

        /// <summary>
        /// 添加TRuleField
        /// </summary>
        /// <param name="ruleFields"></param>
        public void CreateTRuleField(List<TRuleField> ruleFields)
        {
            try
            {
                DaoUtil.ExecuteInsertSQL(ruleFields, this);    //创建规则作用字段
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.CreateRuleError, e.Message, e);
            }
        }

        /// <summary>
        /// 根据StateID删除Rule
        /// </summary>
        /// <param name="ruleID"></param>
        public void DeleteTRuleFieldByForeignKey(string ruleID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, ruleID));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteRuleError, e.Message, e);
            }
        }

        /// <summary>
        /// 根据StateMachineID删除Rule
        /// </summary>
        /// <param name="smID"></param>
        public void DeleteByMetaDataForeignKey(string smID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, MetaDataForeignColumn, smID));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteRuleError, e.Message, e);
            }
        }
    }
}
